package com.bankcomm.ui.specialwidget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.widget.GridView;

/**
 * MyBoderGridView.java
 * @author cheng.l 2013-6-5 下午04:32:41
 */
public class MyBoderGridView extends GridView{
	private int columns =3;
	private Paint mPaint ; 
	public MyBoderGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	public MyBoderGridView(Context context) {
		super(context);
		init();
	}
	
	private void init() {
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setColor(0xffABB8C0);
		
		//虚线
		PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);
		mPaint.setPathEffect(effects);
		mPaint.setStyle(Style.STROKE);
		
	}
	
	/**
	 * 设置GridView列数
	 * @param columns 
	 * */
	public void setColumns(int columns){
		this.columns = columns;
		this.setNumColumns(columns);
	}
	
//	@Override
//	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//		// 设置GridView不滚�?
//		int expandSec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
//				MeasureSpec.AT_MOST);
//		super.onMeasure(widthMeasureSpec, expandSec);
//	}

    @Override
    protected void dispatchDraw(Canvas canvas) {
        final int count = getChildCount();
        final int top = count > 0 ? getChildAt(0).getTop() : 0;
        final int left = count > 0 ? getChildAt(0).getLeft() : 0;
        
        final int mHeight = count > 0? getChildAt(0).getHeight() : 40;
        final int mWidth = count > 0? getChildAt(0).getWidth() : 40;
        
        final int width = getWidth();
        final int height = getHeight();
        
        int lines = 0;
        
        if ( this.getCount() % columns == 0) {
        	lines = this.getCount() / columns;
		} else {
			lines = this.getCount() / columns + 1;
		}
        
        int y = top;
        for (int i = 0; i <= lines; i++) {
        	y = top+i*(mHeight);
        	drawVerticalLine(canvas,width,y);
        }
        //drawVerticalLine(canvas,width,height);
        
        int x = left;
        for(int i = 1; i < columns ;i++){
        	x = left+i*(mWidth);
        	drawHorizontalLine(canvas,height,x);
        }
        super.dispatchDraw(canvas);
    }

    public void drawVerticalLine(Canvas canvas,int width,int y){
    	Path path = new Path();
		path.moveTo(0, y);
		path.lineTo(width, y);
		canvas.drawPath(path, mPaint);
    }
    
    public void drawHorizontalLine(Canvas canvas,int height,int x){
    	Path path = new Path();
		path.moveTo(x, 0);
		path.lineTo(x, height);
		canvas.drawPath(path, mPaint);
    }
}
